package jeecg.business.controller.site;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jeecg.business.entity.site.SiteEntity;
import jeecg.business.service.site.SiteServiceI;
import jeecg.system.service.SystemService;

import org.apache.log4j.Logger;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**   
 * @Title: Controller
 * @Description: 站点
 * @author LinYingPiaoJian
 * @date 2015-06-05 15:58:51
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/siteController")
public class SiteController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SiteController.class);

	@Autowired
	private SiteServiceI siteService;
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}


	/**
	 * 站点列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "site")
	public ModelAndView site(HttpServletRequest request) {
		return new ModelAndView("jeecg/business/site/siteList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(SiteEntity site,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(SiteEntity.class, dataGrid);
		//查询条件组装器
		if(site != null){
			if(StringUtil.isNotEmpty(site.getMc()))
				site.setMc("*"+site.getMc()+"*");
		}
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, site);
		this.siteService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除站点
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(SiteEntity site, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		site = systemService.getEntity(SiteEntity.class, site.getId());
		message = "删除成功";
		siteService.delete(site);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加站点
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(SiteEntity site, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(site.getId())) {
			message = "更新成功";
			SiteEntity t = siteService.get(SiteEntity.class, site.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(site, t);
				siteService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			message = "添加成功";
			siteService.save(site);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		
		return j;
	}

	/**
	 * 站点列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(SiteEntity site, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(site.getId())) {
			site = siteService.getEntity(SiteEntity.class, site.getId());
			if(!"1".equals(site.getRctz())){
				site.setRctz("2");
			}
		}else{
			site = new SiteEntity();
			site.setSxh(999);
			site.setZnbm("2");
			site.setRctz("2");
		}
		if(StringUtil.isEmpty(site.getSydl()))
			site.setSydl("2");
		req.setAttribute("sitePage", site);
		return new ModelAndView("jeecg/business/site/site");
	}
	
	@RequestMapping(params = "toAssignSite")
	public ModelAndView toAssignSite(String keywordid , HttpServletRequest req){
		req.setAttribute("keywordid",keywordid);
		String [] selectids = siteService.getSelectetId(keywordid);
		req.setAttribute("selectids", ","+getSelectedIds(selectids)+",");
		return new ModelAndView("jeecg/business/site/assignsiteList");
	}
	
	String getSelectedIds(String[] selectids){
		String subjectids = "";
		if(selectids != null && selectids.length > 0){
			for(int i=0;i<selectids.length;i++){
				if(subjectids.length() == 0) subjectids = selectids[i];
				else subjectids += ","+selectids[i];
			}
		}
		return subjectids;
	}
	
	/**
	 * 添加课程
	 * 
	 * @return
	 */
	@RequestMapping(params = "assignSite")
	@ResponseBody
	public AjaxJson assignSite(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String keywordid = request.getParameter("keywordid");		
		String ids = request.getParameter("ids");	
		siteService.assignSite(keywordid, ids);
		message = "分配成功";	
		j.setMsg(message);
		String [] selectids = siteService.getSelectetId(keywordid);	
		j.setObj(","+getSelectedIds(selectids)+",");
		return j;
	}
	
	/**
	 * 删除课程
	 * 
	 * @return
	 */
	@RequestMapping(params = "delSite")
	@ResponseBody
	public AjaxJson delSite(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String keywordid = request.getParameter("keywordid");		
		String ids = request.getParameter("ids");	
		siteService.delSite(keywordid, ids);
		message = "解除成功";	
		j.setMsg(message);
		String [] selectids = siteService.getSelectetId(keywordid);
		j.setObj(","+getSelectedIds(selectids)+",");
		return j;
	}
	
	@RequestMapping(params = "getOneSite")
	public String getOneSite() {
		return "jeecg/business/site/getOneSiteList";
	}
	
}
